package study.intconsulting.repository;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.ResultSet;
import java.sql.Driver;

import study.intconsulting.vo.PcInfo;

//Repository
public class Udb {
	
//	public static void dataWrite(PcInfo PcInfo) throws IOException{
//		FileOutputStream myDir	= null;	// ���ϻ�
//		
//		//��¥�� ���� �����
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//		String nowTime = sdf.format(new Date(System.currentTimeMillis()));
//
//		File newFile = new File("C://dataWrite"+nowTime.substring(0, 12)+".txt");	// ������ Ȯ��
//		myDir = new FileOutputStream(newFile, true);
//				
//		String message = PcInfo.getIp()+","+PcInfo.getName()+","+PcInfo.getWho()+","+nowTime+";\n";
//		System.out.println(PcInfo.toString());
//		
//		myDir.write(message.getBytes());	// ���Ͽ� ���� ����	
//		
//	}
	
	// start main
//	public static void main(String[] args) {
//	
//		try {
//			PcInfo PcInfo = new PcInfo();
//			
//			PcInfo.setIp("10.47.123.11");
//			PcInfo.setName("YuJH");
//			PcInfo.setWho("int");
//			
//			Udb.dataWrite(PcInfo);
//	
//		} catch (Throwable e) {
//			e.printStackTrace();
//		}		
//	}
	
	
	// 데이터 입력
	public static void dataWrite(PcInfo PcInfo) throws Exception{
		
		
		Connection conn = getConnection();
		String sql = "INSERT INTO INTSTUDY(IP, NAME, WHO) VALUES(?,?,?)";
		
		if(conn.getAutoCommit()){
			conn.setAutoCommit(false);
		}
		
		System.out.println(sql);
		
		PreparedStatement pstmt = conn.prepareStatement(sql);
		
		pstmt.setString(1, PcInfo.getIp());
		pstmt.setString(2, PcInfo.getName());
		pstmt.setString(3, PcInfo.getWho());
		
		int result = pstmt.executeUpdate();
		
		if(result > 0){
			System.out.println("insert success..");
			conn.commit();
		} else {
			System.out.println("insert fail..");
			conn.rollback();
		}
		
		if(pstmt != null) pstmt.close();
		if(conn != null) conn.close();
		
	}
	
	// Connection 생성
	public static Connection getConnection() throws Exception{
		
		String dirver = "org.apache.derby.jdbc.EmbeddedDriver";
//		String dirver = "org.apache.derby.jdbc.ClientDriver";
		
		String protocol = "jdbc:derby:";
//		String protocol = "jdbc:derby://127.0.0.1:1527/";
		
		
		String startSubfix = "intConsultingStudy;create=true";
		
		
//		"jdbc:derby:intConsultingStudy;create=true"
//		"jdbc:derby:jdbc:derby://127.0.0.1:1527/intConsultingStudy;create=true";
		
		
		Class.forName(dirver);
		Connection conn = DriverManager.getConnection(protocol+startSubfix);
		
		
//		Driver drv = (Driver)Class.forName(dirver).newInstance();
//		Connection conn = drv.connect(protocol+startSubfix, null);
		
		
		
		return conn;
	}
	
	
	// 데이블 생성
	public static void makeTable() throws Exception{
		Connection conn = getConnection();
		StringBuffer sql = new StringBuffer();
		
		sql.append("CREATE TABLE INTSTUDY");
		sql.append("(IP VARCHAR(30), NAME VARCHAR(30), WHO VARCHAR(50))");
		
		System.out.println(sql);
		
		PreparedStatement pstmt = conn.prepareStatement(sql.toString());
		
		pstmt.execute();
		
		
		if(pstmt != null) pstmt.close();
		if(conn != null) conn.close();
		
	}
	
	
	
//	public static void main(String[] arg) throws Exception{
//		//테이블 생성
//		//makeTable();
//		
//		//데이터 입력
//		//PcInfo pcInfo = new PcInfo();
//		//pcInfo.setIp("127.0.0.1");
//		//pcInfo.setName("isMe");
//		//pcInfo.setWho(null);
//		//dataWrite(pcInfo);
//		
//		//데이터 확인
//		//viewData();
//	}
	
	
	// 데이터 확인
	public static void viewData() throws Exception{
		Connection conn = getConnection();
		
		String sql = "SELECT * FROM INTSTUDY";
		System.out.println(sql);
		
		PreparedStatement pstmt = conn.prepareStatement(sql);
		
		ResultSet rs = pstmt.executeQuery();
		
		
		ResultSetMetaData rsmd = pstmt.getMetaData();
		for(int i = 1; i <= rsmd.getColumnCount(); i++){
			System.out.print(rsmd.getColumnName(i));
			System.out.print("\t\t");
		}
		System.out.println();
		
		
		while(rs.next()){
			for(int j = 1; j <= rsmd.getColumnCount(); j++){
				System.out.print(rs.getObject(rsmd.getColumnName(j)));
				System.out.print("\t\t");
			}
			System.out.println();
		}
		
		if(pstmt != null) pstmt.close();
		if(conn != null) conn.close();
	}

}


